package com.honeywell.iotserver.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.honeywell.iotserver.controller.DeviceController;


public class AliClient {
	private static Logger logger = LoggerFactory.getLogger(AliClient.class);
   /**
    * 向阿里发送信息
    * @param url
    * @param json
    * @param encoding
    * @return
    * @throws ClientProtocolException
    * @throws IOException
    */
    public static int sendPostDataByJson(String url, String uuid,int status){
    	
    	
    	logger.info("{URL="+url+",UUID="+uuid+",STATUS="+status+"}");
    	
    	CloseableHttpClient httpclient = null;
        CloseableHttpResponse h_response = null;
		try {
				httpclient = HttpClients.createDefault();
		        //HttpPost http = new HttpPost(url);
				HttpPut http = new HttpPut(url);
				//HttpGet http = new HttpGet(url);
		        List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
		        nvps.add(new BasicNameValuePair("uuid", uuid));  
		        nvps.add(new BasicNameValuePair("device_status", 1+""));  
		        http.setEntity(new UrlEncodedFormEntity(nvps));  
		        h_response = httpclient.execute(http);
		        return h_response.getStatusLine().getStatusCode();
		        
		}catch (Exception e) {
			e.printStackTrace();
			return HttpStatus.SC_INTERNAL_SERVER_ERROR;
		}finally {
			if(httpclient != null) {
				try {
					httpclient.close();
				}catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(h_response!= null) {
				try {
					h_response.close();
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
    }
}